package labox.innovation.gameserver.templates.item;

import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.templates.StatsSet;

import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *  �й���Ʒ����xml �����ļ��Ļ���
 *  
 * @author zjj
 *
 */
public abstract class ParseItemConfig {
	
	static org.slf4j.Logger _log = LoggerFactory.getLogger(ParseItemConfig.class.getName());
	
	protected String _filename;   //�ļ���
	
	//eg: ���� ֧�����2��� Tag��ʽ�� ����һ��
	//ÿ��tag��Ҫһ�������ֶ�
	/**
	 *  <chance mobGrade="0">  //topTag
	 *  	<mobLvl lvlRange="1-10"> //secondTag
	 *  		<set name="talismanChance" val="2000"/>  //set�����࣬ �ֶε���������
	 *     </mobLvl>
	 *  </chance>
	 */
	protected String _root = "list";
	protected String _topTag;     //xml�г���Ԫ�ؽڵ���� ����ڵ�
	protected String _topAttri;   //xml�г���Ԫ�ؽڵ���� ����ڵ�������ֶ�
	
	protected String _secondTag;  //xml�г���Ԫ�ؽڵ���� ��2���ڵ�
	protected String _secondAttri;   //xml�г���Ԫ�ؽڵ���ĵ�2���ڵ�������ֶ�
	
	public void loadConfig()
	{
		File _file = new File(Config.DATAPACK_ROOT, _filename );
		if(! _file.exists() ){
			 _log.warn("�����ļ�:" + _filename + "������" , Config.SERVER_ID );
			return ;
		}
		Document doc;
		try
        {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setIgnoringComments(true);
            doc = factory.newDocumentBuilder().parse(_file);
            
            Node n = doc.getFirstChild();
            
            if( _root.equalsIgnoreCase( n.getNodeName() ) )
            {
            	NodeList chanceList = doc.getElementsByTagName(_topTag);
            	for( int i =0; i < chanceList.getLength(); i++ )
            	{
            		Node node = chanceList.item(i);
            		NamedNodeMap attrs = node.getAttributes();
            		String iMobGrade = attrs.getNamedItem(_topAttri).getNodeValue();
            		
            		if( _secondTag != null )  //����2���ڵ㣬 ����2���ڵ�
            		{
                		NodeList mobLvlList = ( (Element)node ).getElementsByTagName(_secondTag);
                		for( int j=0; j < mobLvlList.getLength(); j++ )
                		{
                			Node lvlnode = mobLvlList.item(j);
                			NamedNodeMap lvlattrs = lvlnode.getAttributes();
                			StatsSet set = new StatsSet();
                			String lvlrange = lvlattrs.getNamedItem(_secondAttri).getNodeValue();
                			set.set(_secondAttri, lvlrange);
                			
                			NodeList setList = ( (Element)lvlnode ).getElementsByTagName("set");
                			
                			for( int k=0; k<setList.getLength(); k++)
                			{
                				parseBeanSet( setList.item( k ), set );
                			}
                			
                			addItemConfig(iMobGrade, set);
                			
                		}// end for( int j=0; j < mobLvlList.getLength(); j++ )
                		
            		}
            		else
            		{
            			NodeList setList = ( (Element)node ).getElementsByTagName("set");
            			StatsSet set = new StatsSet();
            			
            			for( int k=0; k<setList.getLength(); k++)
            			{
            				parseBeanSet( setList.item( k ), set );
            			}
            			
            			addItemConfig(iMobGrade, set);
            		}

            		
            	}// end for( int i =0; i < chanceList.getLength(); i++ )
            	
            }// end  if( "list".equalsIgnoreCase( n.getNodeName() ) )
            
        }
        catch (Exception e)
        {
            _log.error( "Error loading file " + _file,Config.SERVER_ID, e);
            return ;
        }
        
	}// end 
	
    /**
     * ���� node �����ԣ� ���������ֶμ��뵽 set��ȥ
     * @param node
     * @param set
     */
    protected void parseBeanSet( final Node node , final StatsSet set )
    {
    	if( node == null )
    	{
    		return;
    	}
        String name = node.getAttributes().getNamedItem("name").getNodeValue().trim();
        String value = node.getAttributes().getNamedItem("val").getNodeValue().trim();
        
        set.set(name, value);
    }

    
	/**
	 *  ���� ��Ʒ��ص����� ��ÿ������ȥʵ��
	 *  set ���� ���е��ֶ�����
	 *  iType Ϊ��һ���ڵ�� ����ֵ
	 * @param iType
	 * @param set
	 */
	abstract protected void addItemConfig( final String iType, final StatsSet set );
	
}
